/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.book.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.book.entity.BookCategory;
import com.jeesite.modules.book.dao.BookCategoryDao;
import com.jeesite.modules.book.entity.AllBook;
import com.jeesite.modules.book.dao.AllBookDao;

/**
 * book_category 书籍分类表Service
 * @author 逗你玩
 * @version 2019-07-21
 */
@Service
@Transactional(readOnly=true)
public class BookCategoryService extends CrudService<BookCategoryDao, BookCategory> {
	
	@Autowired
	private AllBookDao allBookDao;
	
	/**
	 * 获取单条数据
	 * @param bookCategory
	 * @return
	 */
	@Override
	public BookCategory get(BookCategory bookCategory) {
		BookCategory entity = super.get(bookCategory);
		if (entity != null){
			AllBook allBook = new AllBook(entity);
			allBook.setStatus(AllBook.STATUS_NORMAL);
			entity.setAllBookList(allBookDao.findList(allBook));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param bookCategory 查询条件
	 * @param bookCategory.page 分页对象
	 * @return
	 */
	@Override
	public Page<BookCategory> findPage(BookCategory bookCategory) {
		return super.findPage(bookCategory);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param bookCategory
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(BookCategory bookCategory) {
		super.save(bookCategory);
		// 保存 BookCategory子表
		for (AllBook allBook : bookCategory.getAllBookList()){
			if (!AllBook.STATUS_DELETE.equals(allBook.getStatus())){
				allBook.setCategory(bookCategory);
				if (allBook.getIsNewRecord()){
					allBookDao.insert(allBook);
				}else{
					allBookDao.update(allBook);
				}
			}else{
				allBookDao.delete(allBook);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param bookCategory
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(BookCategory bookCategory) {
		super.updateStatus(bookCategory);
	}
	
	/**
	 * 删除数据
	 * @param bookCategory
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(BookCategory bookCategory) {
		super.delete(bookCategory);
		AllBook allBook = new AllBook();
		allBook.setCategory(bookCategory);
		allBookDao.deleteByEntity(allBook);
	}
	
}